MVC缓存01,使用控制器缓存或数据层缓存 您所在的位置:网站首页 缓存Caching 数据缓存Data Caching MVC缓存01,使用控制器缓存或数据层缓存

MVC缓存01,使用控制器缓存或数据层缓存

2024-05-12 10:29| 来源: 网络整理| 查看: 265

对一些浏览频次多、数据量大的数据,使用缓存会比较好,而对一些浏览频次低,或内容因用户不同的,不太适合使用缓存。

 

在控制器层面,MVC为我们提供了OutputCacheAttribute特性;在数据层使用缓存,用System.Runtime.Caching是不错的选择。

 

  控制器层面使用OutputCacheAttribute缓存

□ OutputCacheAttribute默认的缓存时间是60秒。

[OutputCache(Duration=20, VaryByParam="none")] public ActionResult Index() { ViewBag.Message = DateTime.Now.ToString(); return View(); }

□ 设置缓存位置

缓存的位置通过OutputCacheLocation这个枚举来设置,默认的缓存位置是OutputCacheLocation.Any。OutputCacheLocation其它枚举项包括:Client,Downstream, Server, None, or ServerAndClient。如果我们想把一个与用户有关的信息保存在客户端:

[OutputCache(Duration = 7200, Location = OutputCacheLocation.Client, VaryByParam = "none", NoStore = true)] public ActionResult Index() { ViewBag.Message = "Welcome : " + User.Identity.Name; return View(); }

 

  数据层缓存,通过System.Runtime.Caching

□ 创建一个叫Demo的数据库,并创建表Vehicle。

 

□ 从数据库生成一个"ADO.NET实体数据模型"CachingDemo.edmx

同时,自动生成了继承EF的DbContext的类DemoEntities:

 

并且,在配置文件中自动生成了EF相关内容和连接字符串:

□ 引入System.Runtime.Cache

 

□ 缓存接口

namespace MvcApplication1.Cache { public interface ICacheProvider { object Get(string key); void Set(string key, object data, int cacheTime); bool IsSet(string key); void Invalidate(string key); } }

□ 缓存实现

using System; using System.Runtime.Caching;   namespace MvcApplication1.Cache { public class DefaultCacheProvider : ICacheProvider { private ObjectCache Cache { get { return MemoryCache.Default; } } public object Get(string key) { return Cache[key]; }   public void Set(string key, object data, int cacheTime) { CacheItemPolicy policy = new CacheItemPolicy(); policy.AbsoluteExpiration = DateTime.Now + TimeSpan.FromMinutes(cacheTime); Cache.Add(new CacheItem(key, data), policy); }   public bool IsSet(string key) { return (Cache[key] != null); }   public void Invalidate(string key) { Cache.Remove(key); } } }  

 

□ Vehicle的Repository接口:

using System.Collections.Generic; using MvcApplication1.Models;   namespace MvcApplication1.Repository { public interface IVehicleRepository { void ClearCache(); IEnumerable GetVehicles(); } }  

 

□ Vehicle的Repository接口实现:

using System.Collections.Generic; using System.Linq; using MvcApplication1.Cache; using MvcApplication1.Models; namespace MvcApplication1.Repository { public class VehicleRepository : IVehicleRepository { protected DemoEntities DataContext { get; private set; } public ICacheProvider Cache { get; set; }   public VehicleRepository() : this(new DefaultCacheProvider()) { }   public VehicleRepository(ICacheProvider cacheProvider) { this.DataContext = new DemoEntities(); this.Cache = cacheProvider; }   public void ClearCache() { Cache.Invalidate("vehicles"); }   public System.Collections.Generic.IEnumerable GetVehicles() { IEnumerable vehicles = Cache.Get("vehicles") as IEnumerable; if (vehicles == null) { vehicles = DataContext.Vehicle.OrderBy(v => v.Id).ToList(); if (vehicles.Any()) { Cache.Set("vehicles",vehicles,30); //设置缓存的时间为30分钟 } } return vehicles; } } }  

 

□ HomeController

using System.Web.Mvc; using MvcApplication1.Repository;   namespace MvcApplication1.Controllers { public class HomeController : Controller { public IVehicleRepository Repository { get; set; }   public HomeController(IVehicleRepository repository) { this.Repository = repository; }   public HomeController() : this(new VehicleRepository()) { } public ActionResult Index() { return View(Repository.GetVehicles()); }   [HttpPost] public ActionResult Index(FormCollection form) { Repository.ClearCache(); return RedirectToAction("Index"); } } }  

 

□ Home/Index.cshtml

@model IEnumerable   @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; }   table td { border-collapse: collapse; border: solid 1px black; }     编号 车型 价格 @foreach (var vehicle in Model) { @vehicle.Id.ToString() @vehicle.Name @string.Format("{0:c}",vehicle.Price) }   @using (Html.BeginForm()) { }  

□ 结果

页面第一次加载:

在数据库改变数据,页面内容不变:

点击按钮使缓存失效,视图显示数据库最新数据:

 

  总结

当在数据层使用System.Runtime.Caching,实际上,所有的缓存操作都围绕MemoryCache.Default返回类型为ObjectCache缓存而进行。

□ 参考资料 ※ Understanding Caching in Asp.Net MVC with example ※ DATA CACHING WITH .NET 4.0 AND ASP.NET MVC – PART 1



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有